<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Admin_Workingplaces extends Admin_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->lang->load('workingplace');
	}
	public function ajax_workingplace_get(){
		$workingplaceEntity =  new CI_Working_place_entity();
		$workingplace = $workingplaceEntity->getWorkingPlaceByCode($_REQUEST['code']);
		echo json_encode($workingplace);
	}
	public function ajax_workingplace_list(){
		$workingplaceEntity =  new CI_Working_place_entity();
		$escortSettings = new CI_Escort_setting_entity();
		$defaultLanguageId = $escortSettings->getDefaultLanguageId();
		$workingplaceList = $workingplaceEntity->getWorkingPlaceListByLanguage($defaultLanguageId);
		$data['workingplaceList'] = $workingplaceList;
		
		$languageEntity = new CI_Language_entity();
		$languageList = $languageEntity->getLanguageList();
		$data['languageList'] = $languageList;
		
		$this->load->view('admin/workingplaces/index', $data);
	}
	public function ajax_workingplace_save(){
		$workingplaceNameList = $_REQUEST['workingplace'];
		$workingplaceNameDefault = '';
		foreach ($workingplaceNameList as $key => $value) {
			if($value != null && $value != ""){
				$workingplaceNameDefault = $value;
				break;
			}
		}
		$result = array();
		if($workingplaceNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_workingplace_require');
		}else{
			$code = 0;
			foreach ($workingplaceNameList as $key => $value) {
				$workingplaceEntity =  new CI_Working_place_entity();
				if($value != null && $value != ''){
					$workingplaceEntity->setWorkingPlace($value);
				}else{
					$workingplaceEntity->setWorkingPlace($workingplaceNameDefault);
				}
				$workingplaceEntity->setLanguageId($key);
				$workingplaceEntity->setCode($code);
				$id = $workingplaceEntity->save();
				if($code == 0){
					$code = $id;
					$workingplaceEntity->setId($id);
					$workingplaceEntity->setCode($code);
					$workingplaceEntity->update();
				}
			}
			$result['result'] = true;
			$result['message'] =lang('escort_workingplace_save_success');
			$result['id'] =$id;
		}
		echo json_encode($result);
	}
	public function ajax_workingplace_update(){
		$workingplaceNameList = $_REQUEST['workingplace'];
		$workingplaceIdList = $_REQUEST['workingplace_id'];
		$workingplaceCode = $_REQUEST['workingplace_code'];
		$workingplaceNameDefault = '';
		foreach ($workingplaceNameList as $key => $value) {
			if($value != null && $value != ""){
				$workingplaceNameDefault = $value;
				break;
			}
		}
		$result = array();
		if($workingplaceNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_workingplace_require');
		}else{
			foreach ($workingplaceNameList as $key => $value) {
				$workingplaceEntity =  new CI_Working_place_entity();
				if($value != null && $value != ''){
					$workingplaceEntity->setWorkingPlace($value);
				}else{
					$workingplaceEntity->setWorkingPlace($workingplaceNameDefault);
				}
				$workingplaceEntity->setLanguageId($key);
				$workingplaceEntity->setCode($workingplaceCode);
				$workingplaceEntity->setId($workingplaceIdList[$key]);
				$workingplaceEntity->update();
			}
			$result['result'] = true;
			$result['message'] =lang('escort_workingplace_save_success');
		}
		
		echo json_encode($result);
	}
	public function ajax_workingplace_delete(){
		$result = array();
		$workingplaceEntity =  new CI_Working_place_entity();
		$result['result'] = $workingplaceEntity->deleteByCode($_REQUEST['code']);
		if($result['result']){
			$result['message'] = lang('escort_workingplace_delete_success');
		}else{
			$result['message'] = lang('escort_workingplace_delete_error');
		}
		echo json_encode($result);
	}
}
?>